package kr.ac.kaist.savehearts.status;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import kr.ac.kaist.savehearts.SaveHeartsConstants;
import kr.ac.kaist.savehearts.status.StatusUpdateListener.RequestType;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

public class RequestLocationUpdateTask extends AsyncTask<String, Void, Integer> {
	private String email;
	private double longitude, latitude;
	private StatusUpdateListener listener;
	private Context context;
	private ProgressDialog dialog;
	private final String URL = SaveHeartsConstants.SERVER_URL +	SaveHeartsConstants.URL_DELETE;
	
	public RequestLocationUpdateTask(String email, double longitude, double latitude, Context context, StatusUpdateListener listener) {
		this.email = email;			
		this.longitude = longitude;	this.latitude = latitude;
		this.context = context;		this.listener = listener;
	}
	
	@Override
	protected void onPostExecute(Integer result) {
		if ( this.dialog != null ) this.dialog.dismiss();
		if ( this.listener != null ) 
			this.listener.responseReceived(RequestType.LocationUpdate, result);
	}

	@Override
	protected void onPreExecute() {
		this.dialog = new ProgressDialog(this.context);
		this.dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		this.dialog.setMessage("Please wait...");
		this.dialog.show();
	}

	@Override
	protected Integer doInBackground(String... arg0) {
		// Create a new HttpClient and Post Header
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(this.URL);
		int ret = SaveHeartsConstants.STATUS_OK;
		
		try {
			// Add your data
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(
					new BasicNameValuePair(SaveHeartsConstants.PROP_EMAIL, this.email));
			nameValuePairs.add(
					new BasicNameValuePair(SaveHeartsConstants.PROP_LONGITUDE, 
							String.valueOf(this.longitude)));
			nameValuePairs.add(
					new BasicNameValuePair(SaveHeartsConstants.PROP_LATITUDE, 
							String.valueOf(this.latitude)));
			
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			
			// Execute HTTP Post Request
			HttpResponse response = httpClient.execute(httpPost);
			String respStr = response.getEntity().toString();
			ret = Integer.parseInt(respStr);
			
			// do something with response....
			System.out.println(respStr);
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			ret = SaveHeartsConstants.STATUS_FAIL_UNKNOWN;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			ret = SaveHeartsConstants.STATUS_FAIL_UNKNOWN;
		} catch (IOException e) {
			e.printStackTrace();
			ret = SaveHeartsConstants.STATUS_FAIL_UNKNOWN;
		} 
		
		return ret;
	}
}
